package advanced.sorm;


import advanced.sorm.bean.Columninfo;
import advanced.sorm.bean.TableInfo;
import advanced.sorm.utils.JavaFileUtils;
import advanced.sorm.utils.STringUtils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 负责获取管理数据库左右表结构和类结构的关系
 * 并根据表类型生成类结构
 */

public class TableContext {
    //表明为key, 表信息对象为value
    public static Map<String, TableInfo> tables = new HashMap<>();
    //将po的class对象和表信息对象关联起来, 便于重用
    public static Map<Class, TableInfo> poClassTableMap = new HashMap<>();

    static {
        try {
            Connection conn = DBManager.getConn();
            DatabaseMetaData metaData = conn.getMetaData();
            String dataBase = conn.getCatalog();
            //获取所有的表
            ResultSet tableRet = metaData.getTables(dataBase, "%", "%", new String[]{"TABLE"});
            while (tableRet.next()) {
                String tableName = (String) tableRet.getObject("TABLE_NAME");
                TableInfo tableInfo = new TableInfo(tableName, new HashMap<String, Columninfo>(), new ArrayList<Columninfo>());
                tables.put(tableName, tableInfo);
                //获取表中的字段
                ResultSet columns = metaData.getColumns(dataBase, "%", tableName, "%");
                while (columns.next()) {
                    Columninfo columninfo = new Columninfo(columns.getString("COLUMN_NAME"), columns.getString("TYPE_NAME"), 0);
                    //存起来, 方便重用
                    tableInfo.getColumninfos().put(columns.getString("COLUMN_NAME"), columninfo);
                }
                //查询表中的主键
                ResultSet primaryKeys = metaData.getPrimaryKeys(dataBase, null, tableName);
                while (primaryKeys.next()) {
                    Columninfo ci = (Columninfo) tableInfo.getColumninfos().get(primaryKeys.getObject("COLUMN_NAME"));
                    ci.setKeyType(1);
                    tableInfo.getPriKeys().add(ci);
                }
                if (tableInfo.getPriKeys().size() > 0) {
                    //取唯一主键, 方便使用,  如果是联合主键, 则为空
                    tableInfo.setOnlyPriKey(tableInfo.getPriKeys().get(0));
                }
            }

        } catch (Exception e) {
            //e.printStackTrace();
        }
        //更新po类
        updateJavaPoFile();
    }

    private TableContext() {

    }

    public Map<String, TableInfo> getTableInfos() {
        return tables;
    }

    private static void loadPoTables() {
        for (TableInfo tableInfo : tables.values()) {
            Class<?> clz = null;
            try {
                clz = Class.forName(DBManager.getConf().getPoPackage() + "." +
                        STringUtils.firstChar2Upper(tableInfo.getTname()));
                poClassTableMap.put(clz, tableInfo);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

    }

    private static void updateJavaPoFile() {
        Map<String, TableInfo> tableInfos = tables;
        for (TableInfo tableInfo : tableInfos.values()) {
            JavaFileUtils.createJavaSrc(tableInfo, new MysqlConvertor());
        }
        System.out.println("更新了所有po类");
    }

}
